﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class UIDialog : Singleton<UIDialog>
{
    [CompilerGenerated]
    private static Func<string, string> <>f__am$cache9;
    [SerializeField]
    private GameObject m_canvasGroup;
    [SerializeField]
    private Image m_expressionImage;
    [SerializeField]
    private Transform m_menuButtonGroup;
    [SerializeField]
    private GameObject m_menuButtonPrefabGO;
    [SerializeField]
    private Button m_nextButton;
    [SerializeField]
    private Text m_text;
    [SerializeField]
    private AudioSource m_typingSound;
    private List<UIDialogMenuOptionButton> menuButtonContents = new List<UIDialogMenuOptionButton>();
    private float typingEffectSpeed;

    [DebuggerHidden]
    private IEnumerator _Menu(string[] options, Action<int> resultGetter)
    {
        return new <_Menu>c__Iterator11 { options = options, resultGetter = resultGetter, <$>options = options, <$>resultGetter = resultGetter, <>f__this = this };
    }

    public void Close()
    {
        this.active = false;
    }

    public Coroutine Menu(string[] options, Action<int> resultGetter)
    {
        return base.StartCoroutine(this._Menu(options, resultGetter));
    }

    public void Open()
    {
        this.active = true;
        this.m_text.text = string.Empty;
        this.m_menuButtonGroup.gameObject.SetActive(false);
        this.m_nextButton.gameObject.SetActive(false);
    }

    public Coroutine Say(string contentFormat, params object[] args)
    {
        return this.Say(null, contentFormat, args);
    }

    public Coroutine Say(string speakerName, string contentFormat, params object[] args)
    {
        object[] objArray1 = new object[] { contentFormat, args };
        return this.Say(speakerName, null, objArray1);
    }

    public Coroutine Say(string speakerName, Sprite expression, string contentFormat, params string[] args)
    {
        <Say>c__AnonStorey73 storey = new <Say>c__AnonStorey73 {
            <>f__this = this
        };
        if (contentFormat == null)
        {
            return null;
        }
        contentFormat = contentFormat.Localize();
        if (<>f__am$cache9 == null)
        {
            <>f__am$cache9 = x => x.Localize();
        }
        args = args.Select<string, string>(<>f__am$cache9).ToArray<string>();
        string text = string.Format(contentFormat, (object[]) args);
        this.Open();
        this.m_expressionImage.sprite = expression;
        this.m_expressionImage.gameObject.SetActive(expression != null);
        speakerName = speakerName.Localize();
        storey.speakerNameString = !string.IsNullOrEmpty(speakerName) ? (speakerName + ":\n") : string.Empty;
        this.fastTyping = false;
        return this.StartTypingEffect(text, new Action<string>(storey.<>m__121), new Func<float>(storey.<>m__122), null);
    }

    private void Update()
    {
        if (this.active)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                this.fastTyping = true;
            }
            if (Input.GetKeyUp(KeyCode.Space))
            {
                this.fastTyping = false;
            }
        }
    }

    public bool active
    {
        get
        {
            return base.gameObject.activeSelf;
        }
        set
        {
            base.gameObject.SetActive(value);
        }
    }

    private bool fastTyping
    {
        get
        {
            return (this.typingEffectSpeed == 100f);
        }
        set
        {
            this.typingEffectSpeed = !value ? ((float) 10) : ((float) 100);
        }
    }

    private UIDialogMenuOptionButton menuButtonPrefab
    {
        get
        {
            return this.m_menuButtonPrefabGO.GetOrAddComponent<UIDialogMenuOptionButton>();
        }
    }

    private string text
    {
        get
        {
            return this.m_text.text;
        }
        set
        {
            if (this.m_text.text != value)
            {
                this.m_typingSound.Play();
                this.m_text.text = value;
            }
        }
    }

    [CompilerGenerated]
    private sealed class <_Menu>c__Iterator11 : IDisposable, IEnumerator, IEnumerator<object>
    {
        internal object $current;
        internal int $PC;
        internal string[] <$>options;
        internal Action<int> <$>resultGetter;
        internal UIDialog <>f__this;
        internal int <result>__0;
        internal string[] options;
        internal Action<int> resultGetter;

        internal void <>m__123(UIDialogMenuOptionButton c, int i)
        {
            <_Menu>c__AnonStorey74 storey = new <_Menu>c__AnonStorey74 {
                <>f__ref$17 = this,
                c = c
            };
            if (i == 0)
            {
                EventSystem.current.SetSelectedGameObject(null);
                this.<>f__this.StartWaitForEndOfFrame(new Action(storey.<>m__124));
            }
            storey.c.index = i;
            storey.c.label = this.options[i].Localize();
            storey.c.onClick = new Action(storey.<>m__125);
        }

        [DebuggerHidden]
        public void Dispose()
        {
            this.$PC = -1;
        }

        public bool MoveNext()
        {
            uint num = (uint) this.$PC;
            this.$PC = -1;
            switch (num)
            {
                case 0:
                    this.<>f__this.active = true;
                    this.<result>__0 = -1;
                    if ((this.options == null) || (this.options.Length <= 0))
                    {
                        this.<>f__this.m_menuButtonGroup.gameObject.SetActive(false);
                        break;
                    }
                    this.<>f__this.m_menuButtonGroup.gameObject.SetActive(true);
                    UIUtility.RefreshElements<UIDialogMenuOptionButton>(this.<>f__this.menuButtonContents, this.options.Length, this.<>f__this.menuButtonPrefab, this.<>f__this.m_menuButtonGroup, new Action<UIDialogMenuOptionButton, int>(this.<>m__123));
                    break;

                case 1:
                    break;

                default:
                    goto Label_0120;
            }
            while (this.<result>__0 == -1)
            {
                this.$current = null;
                this.$PC = 1;
                return true;
            }
            if (this.resultGetter != null)
            {
                this.resultGetter(this.<result>__0);
            }
            this.<>f__this.m_menuButtonGroup.gameObject.SetActive(false);
            this.<>f__this.Close();
            this.$PC = -1;
        Label_0120:
            return false;
        }

        [DebuggerHidden]
        public void Reset()
        {
            throw new NotSupportedException();
        }

        object IEnumerator<object>.Current
        {
            [DebuggerHidden]
            get
            {
                return this.$current;
            }
        }

        object IEnumerator.Current
        {
            [DebuggerHidden]
            get
            {
                return this.$current;
            }
        }

        private sealed class <_Menu>c__AnonStorey74
        {
            internal UIDialog.<_Menu>c__Iterator11 <>f__ref$17;
            internal UIDialogMenuOptionButton c;

            internal void <>m__124()
            {
                EventSystem.current.SetSelectedGameObject(this.c.gameObject);
            }

            internal void <>m__125()
            {
                this.<>f__ref$17.<result>__0 = this.c.index;
            }
        }
    }

    [CompilerGenerated]
    private sealed class <Say>c__AnonStorey73
    {
        internal UIDialog <>f__this;
        internal string speakerNameString;

        internal void <>m__121(string x)
        {
            this.<>f__this.text = this.speakerNameString + x;
        }

        internal float <>m__122()
        {
            return this.<>f__this.typingEffectSpeed;
        }
    }
}

